package com.koo.modules.base.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.koo.common.exception.RRException;
import com.koo.common.utils.PageUtils;
import com.koo.common.utils.Query;
import com.koo.modules.base.dao.BaseDictDao;
import com.koo.modules.base.dao.BaseDictItemDao;
import com.koo.modules.base.entity.BaseDictEntity;
import com.koo.modules.base.entity.BaseDictItemEntity;
import com.koo.modules.base.entity.vo.DictVo;
import com.koo.modules.base.service.BaseDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


@Service
public class BaseDictServiceImpl extends ServiceImpl<BaseDictDao, BaseDictEntity> implements BaseDictService {

    @Resource
    private BaseDictItemDao dictItemDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String key = String.valueOf(params.get("key"));
        LambdaQueryWrapper<BaseDictEntity> wrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotEmpty(key)){
            wrapper.and(w -> w.like(BaseDictEntity::getName, key)
                    .or()
                    .like(BaseDictEntity::getCode, key)
            );
        }
        IPage<BaseDictEntity> page = this.page(
                new Query<BaseDictEntity>().getPage(params),
                wrapper
        );
        return new PageUtils(page);
    }

    @Override
    public List<DictVo> getDict(String code) {
        return baseMapper.getDict(code);
    }

    @Override
    public List<BaseDictEntity> listDict(Map<String, Object> params) {
        String key = String.valueOf(params.get("key"));
        LambdaQueryWrapper<BaseDictEntity> wrapper = new LambdaQueryWrapper();
        if(StringUtils.isNotEmpty(key)){
            wrapper.and(w -> w.like(BaseDictEntity::getName, key)
                    .or()
                    .like(BaseDictEntity::getCode, key)
            );
        }
        return baseMapper.selectList(wrapper);
    }

    @Override
    public List<BaseDictItemEntity> listDictItem(Long dictId) {
        return dictItemDao.selectList(new LambdaQueryWrapper<BaseDictItemEntity>().eq(dictId != null, BaseDictItemEntity::getDictId, dictId));
    }

    @Override
    @Transactional
    public void updateStatus(Long id, Integer status) {
        if(id == null){
            throw new RRException("请选择要更新的行!");
        }
        BaseDictEntity dict = new BaseDictEntity();
        dict.setDictId(id);
        dict.setStatus(status);
        baseMapper.updateById(dict);
        //更新所有子节点状态
        BaseDictItemEntity item = new BaseDictItemEntity();
        LambdaUpdateWrapper<BaseDictItemEntity> wrapper = new LambdaUpdateWrapper<BaseDictItemEntity>();
        wrapper.eq(BaseDictItemEntity::getDictId, id);
        wrapper.set(BaseDictItemEntity::getStatus, status);
        dictItemDao.update(item, wrapper);
    }

    @Override
    public void updateItemStatus(Long id, Integer status) {
        if(id == null){
            throw new RRException("请选择要更新的行!");
        }
        BaseDictItemEntity dictItemEntity = new BaseDictItemEntity();
        dictItemEntity.setItemId(id);
        dictItemEntity.setStatus(status);
        dictItemDao.updateById(dictItemEntity);
    }

    @Override
    public void batchDelete(Long[] ids) {
        if(ids != null && ids.length > 0){
            LambdaQueryWrapper<BaseDictItemEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(BaseDictItemEntity::getDictId, ids);
            List<BaseDictItemEntity> itemList = dictItemDao.selectList(wrapper);
            if(!CollectionUtils.isEmpty(itemList)){
                throw new RRException("请先删除对应的字典明细！");
            }
        }
        this.removeByIds(Arrays.asList(ids));
    }

}